package com.arica.project.common;

import com.alibaba.fastjson.JSONObject;
import com.arica.common.constant.Constants;
import com.arica.common.utils.IdUtils;
import com.arica.common.utils.VerifyCodeUtils;
import com.arica.common.utils.sign.Base64;
import com.arica.framework.redis.RedisCache;
import com.arica.framework.web.domain.AjaxResult;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 验证码操作处理
 * 
 * @author soo
 */
@RestController
public class CaptchaController
{
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private DefaultMQProducer defaultMQProducer;
    @Autowired
    private BBossESStarter bbossESStarterDefault;

    /**
     * 生成验证码
     */
    @GetMapping("/captchaImage")
    public AjaxResult getCode(HttpServletResponse response) throws IOException
    {
        //验证环境,获取es状态
        String es_response = bbossESStarterDefault.getRestClient().executeHttp("_cluster/state?pretty", ClientInterface.HTTP_GET);
        System.out.println(es_response);

        //判断索引类型是否存在，false表示不存在，正常返回true表示存在
        boolean exist = bbossESStarterDefault.getRestClient().existIndiceType("twitter","tweet");
        System.out.println("default twitter/tweet:"+exist);
        //获取logs对应的Elasticsearch集群客户端，并进行existIndiceType操作
        exist = bbossESStarterDefault.getRestClient("logs").existIndiceType("twitter","tweet");
        System.out.println("logs twitter/tweet:"+exist);


//        try {
//            SendResult sendResult = defaultMQProducer.send(new Message("topic_syn", "*", "123".getBytes()));
//            System.err.println(JSONObject.toJSONString(sendResult));
//        } catch (MQClientException e) {
//            e.printStackTrace();
//        } catch (RemotingException e) {
//            e.printStackTrace();
//        } catch (MQBrokerException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        // 生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        // 唯一标识
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;

        redisCache.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        // 生成图片
        int w = 111, h = 36;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        VerifyCodeUtils.outputImage(w, h, stream, verifyCode);
        try
        {
            AjaxResult ajax = AjaxResult.success();
            ajax.put("uuid", uuid);
            ajax.put("img", Base64.encode(stream.toByteArray()));
            return ajax;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        finally
        {
            stream.close();
        }
    }
}
